package com.ruoyi.standingbook.sanitationvehicle.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.esotericsoftware.minlog.Log;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import com.ruoyi.standingbook.sanitationvehicle.domain.SanitationVehicleImportBase;
import com.ruoyi.standingbook.sanitationvehicle.domain.bo.SanitationVehicleImportBaseBo;
import com.ruoyi.standingbook.sanitationvehicle.domain.vo.SanitationVehicleImportBaseVo;
import com.ruoyi.standingbook.sanitationvehicle.mapper.SanitationVehicleImportBaseMapper;
import com.ruoyi.standingbook.sanitationvehicle.service.ISanitationVehicleImportBaseService;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Collection;

/**
 * 环卫作业车导入Service业务层处理
 *
 * @author lq
 * @date 2024-01-03
 */
@RequiredArgsConstructor
@Service
public class SanitationVehicleImportBaseServiceImpl implements ISanitationVehicleImportBaseService {

	private final SanitationVehicleImportBaseMapper baseMapper;

	/**
	 * 查询环卫作业车导入
	 */
	@Override
	public SanitationVehicleImportBaseVo queryById(String batchNo) {
		return baseMapper.selectVoById(batchNo);
	}

	/**
	 * 查询环卫作业车导入列表
	 */
	@Override
	public TableDataInfo<SanitationVehicleImportBaseVo> queryPageList(SanitationVehicleImportBaseBo bo,
			PageQuery pageQuery) {
		LambdaQueryWrapper<SanitationVehicleImportBase> lqw = buildQueryWrapper(bo);
		Page<SanitationVehicleImportBaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

	/**
	 * 查询环卫作业车导入列表
	 */
	@Override
	public List<SanitationVehicleImportBaseVo> queryList(SanitationVehicleImportBaseBo bo) {
		LambdaQueryWrapper<SanitationVehicleImportBase> lqw = buildQueryWrapper(bo);
		return baseMapper.selectVoList(lqw);
	}

	
	private LambdaQueryWrapper<SanitationVehicleImportBase> buildQueryWrapper(SanitationVehicleImportBaseBo bo) {
		LambdaQueryWrapper<SanitationVehicleImportBase> lqw = Wrappers.lambdaQuery();

		lqw.eq(bo.getBatchNo() != null, SanitationVehicleImportBase::getBatchNo, bo.getBatchNo());
		lqw.eq(bo.getProcessStatus() != null, SanitationVehicleImportBase::getProcessStatus, bo.getProcessStatus());
		lqw.eq(StringUtils.isNotBlank(bo.getProcessMsg()), SanitationVehicleImportBase::getProcessMsg,
				bo.getProcessMsg());
		lqw.eq(bo.getAttachMsg() != null, SanitationVehicleImportBase::getAttachMsg, bo.getAttachMsg());
		lqw.eq(StringUtils.isNotBlank(bo.getExcelUrl()), SanitationVehicleImportBase::getExcelUrl, bo.getExcelUrl());
		
		lqw.ge(StringUtils.isNotBlank(bo.getCreateDateBegin()), SanitationVehicleImportBase::getCreateTime,
				bo.getCreateDateBegin());
		
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
		    lqw.lt(StringUtils.isNotBlank(bo.getCreateDateEnd()), SanitationVehicleImportBase::getCreateTime,
		    		DateUtils.addDay(sdf.parse(bo.getCreateDateEnd()), 1));
		} catch (Exception e) {
			Log.error("时间解析错误，原因：" + e.getMessage());
		}
		
		lqw.orderByDesc(SanitationVehicleImportBase::getCreateTime);
		
		return lqw;
	}

	/**
	 * 新增环卫作业车导入
	 */
	@Override
	public Boolean insertByBo(SanitationVehicleImportBaseBo bo) {
		SanitationVehicleImportBase add = BeanUtil.toBean(bo, SanitationVehicleImportBase.class);
		validEntityBeforeSave(add);
		boolean flag = baseMapper.insertOrUpdate(add);
		if (flag) {
			bo.setBatchNo(add.getBatchNo());
		}
		return flag;
	}

	/**
	 * 修改环卫作业车导入
	 */
	@Override
	public Boolean updateByBo(SanitationVehicleImportBaseBo bo) {
		SanitationVehicleImportBase update = BeanUtil.toBean(bo, SanitationVehicleImportBase.class);
		validEntityBeforeSave(update);
		return baseMapper.updateById(update) > 0;
	}

	/**
	 * 保存前的数据校验
	 */
	private void validEntityBeforeSave(SanitationVehicleImportBase entity) {
		// TODO 做一些数据校验,如唯一约束
	}

	/**
	 * 批量删除环卫作业车导入
	 */
	@Override
	public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
		if (isValid) {
			// TODO 做一些业务上的校验,判断是否需要校验
		}
		return baseMapper.deleteBatchIds(ids) > 0;
	}
}
